var app = getApp();
import { request } from '../../utils/request.js'
Page({
  data: {
    showType: 'changeTel',
    isShowModal: false,
    userInfo: {},
    isShowgetuser:false,
    topstyle:2,
    type:2
  },
  onLoad: function () {
    var that = this
    app.setVersion(that);
    if (!app.globalData.plumSession) {
      //回调
      app.plumSessionCallback = plumSession => {
        if (plumSession != '') {
          console.log('查询到登录信息');
          that.getSlient();
        } else {
          console.log('未查询到登录信息');
          app.wechatSq(that);
        }
      }
    } else {
      that.getSlient();
    }
  },
  onShow:function(){
    var that = this;
    app.setNavtitle('我');
    that.setData({
      userInfo: app.globalData.userInfo ? app.globalData.userInfo:''
    });
    if (wx.getStorageSync('appletad')){
      that.setData({
        appletad: true
      });
    }else{
      that.setData({
        appletad: false
      });
    }
    if (!app.globalData.plumSession) {
      app.wechatSq(that);
    }else{
      that.requestMine();
      app.contactData(that); 
    }
    that.setData({
      followOpen: app.globalData.followOpen ? app.globalData.followOpen : 0
    })
    app.setCartnum();//更新购物车数量
  },
  initPage:function(){
    var that = this;
    console.log("页面初始化")
    that.onLoad();
    that.onShow();
  },
  getSlient: function () {
    var that = this;
    var slient = app.globalData.slient ? app.globalData.slient : '';
    that.setData({
      slient: slient
    })
    if (slient == 1) {
      that.setData({
        isShowgetuser: true
      })
      // wx.hideTabBar()
    }
  },
  // 客服回复组件间传值
  getreplyData: function (e) {
    console.log(e.detail);
    this.setData(e.detail);
  },
  requestMine:function(){
    var that = this;
    request({
      pointer: { page: that, fname: "requestMine" },
      data: {
        map: 'applet_member_center_cfg',
      },
    }).then(res => {
      if (res.data.ec == 200) {
        console.log(res.data);
        that.setData({
          mineInfo: res.data.data,
          topstyle: res.data.data.topstyle ? res.data.data.topstyle : that.data.topstyle
        })
        // wx.setNavigationBarTitle({
        //   title: res.data.data.title
        // })
      } else {
        app.errorTip(that, res.data.em, 2000);
      }
    }).catch(res => {

    })
    // var data = {};
    // data.map = 'applet_member_center_cfg';
    // //发起请求，获取列表列表
    // wx.showToast({
    //   title: '加载中',
    //   icon: 'loading',
    //   mask: true,
    //   duration: 10000
    // });
    // wx.request({
    //   url: app.globalData.requestUrl,
    //   data: data,
    //   success: function (res) {
    //     console.log(res.data);
    //     if (res.data.ec == 200) {
    //       that.setData({
    //         mineInfo: res.data.data,
    //         topstyle: res.data.data.topstyle ? res.data.data.topstyle : that.data.topstyle
    //       })
    //       // wx.setNavigationBarTitle({
    //       //   title: res.data.data.title
    //       // })
    //     } else {
    //       app.errorTip(that, res.data.em, 2000);
    //     }
    //   },
    //   complete: function () {
    //     wx.hideToast();
    //     wx.stopPullDownRefresh();
    //   }
    // });
  },
  onPullDownRefresh:function(){
    var that = this;
    that.requestMine();
  },
  myAddress: function () {
    wx.navigateTo({
      url: '/pages/addressManage/addressManage'
    })
  },
  openOrder: function (e) {
    var type = e.currentTarget.dataset.type;
    console.log(type)
    wx.navigateTo({
      url: '/pages/myorderTab/myorderTab?type='+type
      // url: '/subpages/myorder/myorder?type='+type
    })
  },
  mycart:function(){
    wx.navigateTo({
      url: '/pages/mycartTab/mycartTab'
    })
  },
  contactKefu: function () {
    console.log('检测是否已登录');
    var that = this;
    wx.showModal({
      title: '',
      content: '联系客服请先登录天店通账号哦~',
      cancelText: '我知道了',
      confirmText: '立即登录',
      confirmColor: '#48C23D',
      success: function (res) {
        if (res.confirm) {
          that.setData({
            isShowModal: true
          })
        }
      }
    })
  },
  hideModal: function () {
    this.setData({
      isShowModal: false
    })
  },
  showOpera: function (e) {
    var type = e.target.dataset.type;
    var showtype = "";
    var that = this;
    if (type == 'confirmTel') {
      var isReg = true;
      if (isReg) {
        showtype = 'logPart';
      } else {
        showtype = 'regPart';
      }
      console.log("检测是否注册");
    } else {
      showtype = type;
    }
    that.setData({
      showType: showtype
    })
  },
  myjfshop: function () {//积分商城
    wx.navigateTo({
      url: '/subpages/integralShop/integralShop'
    })
  },
  mypt: function () {//我的拼团
    wx.navigateTo({
      url: '/subpages/myGroup/myGroup'
    })
  },
  mycjpt: function () {//我的抽奖拼团
    wx.navigateTo({
      url: '/subpages0/mycjGroup/mycjGroup'
    })
  },
  mykj: function () {//我的砍价
    wx.navigateTo({
      url: '/subpages/bargainOrder/bargainOrder'
    })
  },
  myms: function () {//我的秒杀
    wx.navigateTo({
      url: '/subpages/seckillOrder/seckillOrder'
    })
  },
  tomyProfile: function (e) {// 我的资料
    console.log("编辑个人资料")
    wx.navigateTo({
      url: '/pages/myProfile/myProfile'
    })
  },
  // 我的钱包
  toMyWallet: function (e) {
    var title = '';
    if (e.currentTarget.dataset.title) {
      title = e.currentTarget.dataset.title;
    }

    // todo wanyiming
    if (app.globalData.checkData.check) {
      return false;
    }
    wx.navigateTo({
      url: '/pages/myWallet/myWallet?title=' + title
    })
  },
  tohelpCenter: function (e) {
    var title = '';
    if (e.currentTarget.dataset.title) {
      title = e.currentTarget.dataset.title;
    }
    wx.navigateTo({
      url: '/subpages0/helpCenter/helpCenter?title=' + title
    })
  },
  // 优惠券
  toMycoupon: function () {
    wx.navigateTo({
      url: '/subpages/myCoupon/myCoupon'
    })
  },
  // 分销中心
  todistributionCenter: function (e) {
    var name = e.currentTarget.dataset.name;
    wx.navigateTo({
      url: '/subpages0/distributionCenter/distributionCenter?title='+name
    })
  },
  // 分享收益
  todistribProfit: function (e) {
    var title = e.currentTarget.dataset.name;
    wx.navigateTo({
      url: '/subpages0/profitCenter/profitCenter?title=' + title
    })
  },
  // 单品分销
  todistribGoods: function (e) {
    var title = e.currentTarget.dataset.name;
    wx.navigateTo({
      url: '/subpages0/goodDistribution/goodDistribution?title=' + title
    })
  },
  // 小金库
  toMybank: function (e) {
    var title = e.currentTarget.dataset.title;
    wx.navigateTo({
      url: '/subpages/myBank/myBank?title=' + title
    })
  },
  // 卡包
  toCardpage: function () {
    
    wx.navigateTo({
      url: '/subpages/memberCard/memberCard'
    })
  },
  //微信计步
  toStepPoint: function (e) {
    var title = e.currentTarget.dataset.title;
    wx.navigateTo({
      url: '/subpages0/stepPoint/stepIndex/stepIndex?title=' + title,
    })
  },
  toCard:function(e){
    var type = e.currentTarget.dataset.type;
    if(type==0){
      wx.navigateTo({
        url: '/subpages/memberCard/memberCard'
      })
    } else if( type==1 ){
      wx.navigateTo({
        url: '/subpages/buyMenberCard/buyMenberCard'
      })
    } else if (type == 2) {
      wx.navigateTo({
        url: '/subpages/storeMemberPage/storeMemberPage'
      })
    } else if (type == 3) {
      wx.navigateTo({
        url: '/subpages/walletRecharge/walletRecharge?from=slide'
      })
    }
  },
  // 会员特权
  toVippage: function () {
    wx.navigateTo({
      url: '/subpages/vipRights/vipRights'
    })
  },
  //订阅中心
  toSubscribe:function(e){
    var title = e.currentTarget.dataset.title;
    wx.navigateTo({
      url: '/subpages0/subscribeNews/subscribeNews?title='+title,
    })
  },
  toTechnicalPage: function (e) {
    var that = this;
    var onoff = e.currentTarget.dataset.onoff;
    console.log(onoff);
    if(onoff==2){
      wx.setStorageSync('appletad', true);
      that.setData({
        appletad:true
      })
    }
    if (onoff) {
      wx.navigateTo({
        url: '/pages/technicalPage/technicalPage'
      })
    }
  },
  //打开设置页面
  openSettingPage: function () {
    var that = this;
    wx.navigateTo({
      url: '/subpages1/settingPage/settingPage',
    })
  },
  toincomeWithdraw: function (e) {// 申请提现
    wx.navigateTo({
      url: '/pages/incomeWithdraw/incomeWithdraw'
    })
  },
  makeCall: function () {
    app.makeCall();
  },
  openPayread: function () {
    wx.navigateTo({
      url: '/subpages/payRead/payRead'
    })
  },
  tocollectBrowse:function(e){
    var type = e.currentTarget.dataset.type;
    var title = e.currentTarget.dataset.name;
    wx.navigateTo({
      url: '/subpages/collectBrowse/collectBrowse?type='+type+'&title='+title
    })
  },
  // 组队领红包入口
  toRedbagshow:function(){
    wx.navigateTo({
      url: '/subpages0/teamRedpacket/teamIndex/teamIndex',
    })
  },
  toGeReward: function () {
    var that = this;
    wx.navigateTo({
      url: '/subpages/getReward/getReward',
    })
  },
  // 我的返现订单
  toFxorder: function () {
    wx.navigateTo({
      url: '/subpages0/fxOrder/fxOrder',
    })
  },
  hideGetuser:function(){
    var that = this;
    that.setData({
      isShowgetuser: false
    })
  },
  // 获取用户信息
  getuserInfo: function (e) {
    var that = this;
    if (e.detail.userInfo) {
      var userdata = e.detail;
      console.log(userdata);
      var slient = that.data.slient;
      var again = false;
      if (slient == 0) {
        again = true;
      }
      app.getuserInfo(that, userdata, again);
    }
  },
  getPhoneNumber: function (e) { 
    var that = this;
    wx.login({
      success: function (res) {
        if (res.code) {
          var data = {
            map: 'applet_bind_member_mobile',
            code: res.code,
            encryptedData: e.detail.encryptedData,
            iv: e.detail.iv
          };
          if (data.encryptedData && data.iv){
            that.requestMobile(data);
          }
        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      },
      fail:function(res){
        console.log(res);
      }
    });
  },
  requestMobile: function (data) {
    console.log(data);
    var that = this;
    //发起请求，获取列表列表
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      mask: true,
      duration: 10000
    });
    console.log(app.globalData.requestUrl);
    console.log(data);
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          console.log(res.data.data);
          var mobile = res.data.data;
          var mineInfo = that.data.mineInfo;
          console.log(mineInfo);
          mineInfo.myphoneNum = mobile;
          that.setData({
            mineInfo: mineInfo
          })
        } else {
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideToast();
        wx.stopPullDownRefresh();
      }
    });
  },
  myorderClick: function (e) {
    var title = e.currentTarget.dataset.title;
    wx.navigateTo({
      url: '/subpages/Generalreservationlist/Generalreservationlist?title=' + title,
    })
  },
})
